<template>
  <div class="app">
    <div class="search-bar">
      <el-form :model="searchData" label-width="80px" style="padding: 0 20px;">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="公司名称" style="margin-bottom: 5px;">
              <el-select v-model="searchData.cmhkId"
                size="medium"
                filterable
                clearable
                class="select-item"
                remote
                reserve-keyword
                :remote-method="remoteMethod"
                @change="selectCompany"
                placeholder="请输入字段搜索">
                <el-option
                  v-for="item in companyNameOption"
                  :key="item.code"
                  :label="item.value"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div style="margin-top: 3px;">
              <el-button icon="el-icon-search" type="primary" size="small" @click="handleSearch">查询</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="add-form">
      <el-button icon="el-icon-plus" v-show="tableOption.isShowOperate" type="primary" size="small" @click="handleAdd">新增</el-button>
    </div>
    <div ref="ref" :class="tableOption.isShowOperate?'table-box':'table-box2'">
      <TableComponent :columns="securitiesAccountColumns" :height="height" :data="tableData" @change="onTableChange" :tableOption="tableOption" :page-option="searchData.page" :stripe="stripe" :total="total">
        <template slot-scope="scope">
          <el-button @click="seeRow(scope.scope.row)" type="text" size="mini">查看</el-button>
          <el-button @click="editRow(scope.scope.row)" type="text" size="mini">修改</el-button>
          <el-button @click="deleteRow(scope.scope.row)" type="text" size="mini">删除</el-button>
        </template>
      </TableComponent>
    </div>
    <el-dialog :title="title" :visible.sync="dialogVisibleAdd" width="60%" :before-close="handleCancle">
      <div class="dialog-box">
        <el-form ref="form" :model="addForm" label-width="155px" :rules="rules" label-position="left" style="padding: 0 20px;" :disabled="disabled">
          <el-row :gutter="30">
            <el-col :span="12">
              <el-form-item label="证券账户类别" prop="accountType" style="margin-bottom: 12px; margin-left: -9px">
                <el-select v-model="addForm.accountType" size="small" class="select-item" style="width: calc(100% - 9px); margin-left: 9px">
                  <el-option
                    v-for="item in accountTypeOption"
                    :key="item.index"
                    :label="item.value"
                    :value="item.code">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="交易账户密码负责人" prop="transPasswordUser" style="margin-bottom: 12px;">
                <el-input v-model="addForm.transPasswordUser" size="small"></el-input>
              </el-form-item>
              <el-form-item label="股票存管机构" prop="stockDepository" style="margin-bottom: 12px;">
                <el-input v-model="addForm.stockDepository" size="small"></el-input>
              </el-form-item>
              <el-form-item label="股票托管机构" prop="stockCustody" style="margin-bottom: 12px;">
                <el-input v-model="addForm.stockCustody" size="small"></el-input>
              </el-form-item>
              <el-form-item label="证券账户号码" prop="accountNum" style="margin-bottom: 12px; margin-left: -9px">
                <el-input v-model="addForm.accountNum" size="small" :disabled="ifEdit" style="width: calc(100% - 9px); margin-left: 9px"></el-input>
              </el-form-item>
              <el-form-item label="开户日期" prop="startDate" style="margin-bottom: 12px; margin-left: -9px">
                <el-date-picker :picker-options="pickerOptions" class="select-item" size="small" v-model="addForm.startDate" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" style="width: calc(100% - 9px); margin-left: 9px"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="证券账户名称" prop="accountName" style="margin-bottom: 12px; margin-left: -9px">
                <el-input v-model="addForm.accountName" size="small" style="width: calc(100% - 9px); margin-left: 9px"></el-input>
              </el-form-item>
              <el-form-item label="资金账户密码负责人" prop="fundsPasswordUser" style="margin-bottom: 12px;">
                <el-input v-model="addForm.fundsPasswordUser" size="small"></el-input>
              </el-form-item>
              <el-form-item label="账户管理单位" prop="accountManagement" style="margin-bottom: 12px;">
                <el-input v-model="addForm.accountManagement" size="small"></el-input>
              </el-form-item>
              <el-form-item label="股票托管期限" prop="stockCustodyDate" style="margin-bottom: 12px;">
                <el-date-picker class="select-item" size="small" v-model="addForm.stockCustodyDate" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
              <el-form-item label="是否设置国有股东标记" prop="isStateShareholder" style="margin-bottom: 12px;">
                <el-select v-model="addForm.isStateShareholder" size="small" class="select-item">
                  <el-option
                    v-for="item in isStateShareholderOption"
                    :key="item.index"
                    :label="item.value"
                    :value="item.code">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="销户日期" prop="endDate" style="margin-bottom: 12px;">
                <el-date-picker :picker-options="pickerOptions" class="select-item" size="small" v-model="addForm.endDate" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button v-if="!disabled" type="primary" @click="handleSubmit('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import TableComponent from '@/components/Table/table'
import { queryCompanyByName, addSecuritiesAccount, securitiesType, querySecurities, delSecurities, editSecurities, querySingleSecurities } from '@/api/modules/propertyRight.js'
import { securitiesAccountColumns } from './index.js'
export default {
  components: { TableComponent },
  data() {
    return {
      searchData: {
        cmhkId: '',
        page: {
          pageNum: 1,
          pageSize: 10
        }
      },
      securitiesAccountColumns,
      tableData: [],
      height: null,
      title: '',
      total: 0,
      tableOption: {
        isShowOperate: true,
        operateWidth: '100px'
      },
      stripe: true,
      dialogVisibleAdd: false,
      addForm: {
        cmhkId: '',
        accountType: '',
        transPasswordUser: '',
        stockDepository: '',
        stockCustody: '',
        accountNum: '',
        startDate: '',
        accountName: '',
        fundsPasswordUser: '',
        accountManagement: '',
        stockCustodyDate: '',
        isStateShareholder: '',
        endDate: ''
      },
      accountTypeOption: [],
      rules: {
        accountType: [{ required: true, message: '请选择证券账户类别', trigger: 'change' }],
        accountName: [{ required: true, message: '请输入证券账户名称', trigger: 'blur' }],
        accountNum: [{ required: true, message: '请输入证券账户号码', trigger: 'blur' }],
        startDate: [{ required: true, message: '请选择开户日期', trigger: 'blur' }],
      },
      disabled: false,
      companyNameOption: [],
      isStateShareholderOption: [
        { value: '是', code: '1' },
        { value: '否', code: '0' }
      ],
      ifEdit: false,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now() - 8.64e6 // 如果没有后面的-8.64e6就是不可以选择今天的
        }
      }
    }
  },
  mounted() {
    const key = this.$store.state.key.resourceCode
    if (key === '0') {
      this.tableOption.isShowOperate = false
    } else {
      this.tableOption.isShowOperate = true
    }
    this.$nextTick(() => {
      this.height = this.$refs.ref.offsetHeight
    })
    window.onresize = () => {
      this.height = this.$refs.ref.offsetHeight
    }
    securitiesType().then(res => {
      this.accountTypeOption = res.data.body
    })
  },
  destroyed(){
    window.onresize = null
  },
  methods: {
    remoteMethod(query) {
      const data = {
        chiName: query
      }
      queryCompanyByName(data).then(res => {
        if (res.data.code === '0') {
          this.companyNameOption = res.data.body
        }
      })
    },
    selectCompany(val) {
      this.searchData.cmhkId = val
      this.addForm.cmhkId = val
      this.handleSearch()
      if (val === '') {
        this.tableData = []
      }
    },
    handleSearch() {
      if (this.searchData.cmhkId === '') {
        this.$message.error('请先选择公司')
      } else {
        querySecurities(this.searchData).then(res => {
          if (res.data.code === '0') {
            this.tableData = res.data.body.list
            this.total = res.data.body.total
          } else {
            return false
          }
        })
      }
    },
    // 点击新增
    handleAdd() {
      if (this.searchData.cmhkId === '') {
        this.$message.error('请先选择公司')
      } else {
        this.dialogVisibleAdd = true
        this.title = '新增'
        this.ifEdit = false
        this.disabled = false
        this.addForm.cmhkId = this.searchData.cmhkId
      }
    },
    // 点击确定新增/修改
    handleSubmit(formName) {
      if (this.title === '新增') {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            addSecuritiesAccount(this.addForm).then(res => {
              if (res.data.code === '0') {
                this.$message.success('新增成功')
                this.resetForm('form')
                this.dialogVisibleAdd = false
                this.handleSearch()
              } else {
                this.$message.error(res.data.message)
              }
            })
          } else {
            return false
          }
        })
      } else if (this.title === '修改') {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            editSecurities(this.addForm).then(res => {
              if (res.data.code === '0') {
                this.$message.success('修改成功')
                this.resetForm('form')
                this.dialogVisibleAdd = false
                this.handleSearch()
              } else {
                this.$message.error(res.data.message)
              }
            })
          } else {
            return false
          }
        })
      }
    },
    editRow(row) {
      this.dialogVisibleAdd = true
      this.disabled = false
      this.title = '修改'
      this.ifEdit = true
      const data = {
        accountNum: row.accountNum,
        cmhkId: row.cmhkId
      }
      querySingleSecurities(data).then(res => {
        if (res.data.code === '0') {
          this.addForm = res.data.body
        }
      })
    },
    seeRow(row) {
      this.dialogVisibleAdd = true
      this.disabled = true
      this.title = '查看'
      this.ifEdit = true
      const data = {
        accountNum: row.accountNum,
        cmhkId: row.cmhkId
      }
      querySingleSecurities(data).then(res => {
        if (res.data.code === '0') {
          this.addForm = res.data.body
        }
      })
    },
    // 删除
    deleteRow(row) {
      console.log(row)
      this.$confirm('确认删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          accountNum: row.accountNum,
          cmhkId: row.cmhkId
        }
        delSecurities(data).then(res => {
          if (res.data.code === '0') {
            this.$message.success('删除成功')
            this.handleSearch()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })     
      })
    },
    // 分页
    onTableChange(val) {
      this.searchData.page = val
      this.handleSearch()
    },
    handleClear() {
      this.searchData.companyChiName = ''
      this.searchData.cmhkId = ''
    },
    handleCancle() {
      this.resetForm('form')
      this.dialogVisibleAdd = false
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
    
  }
}
</script>

<style lang="less" scoped>
  .app {
    height: 100%;
    .search-bar {
      width: calc(100% - 60px);
      background-color: #ffffff;
      margin-bottom: 10px;
      padding: 10px 30px;
    }
    .add-form {
      padding: 10px 20px;
      background-color: #ffffff;
    }
    .table-box {
      width: 100%;
      height: calc(100vh - 260px);
      .table {
        height: 100%;
      }
    }
    .table-box2 {
      width: 100%;
      height: calc(100vh - 230px);
      .table {
        height: 100%;
      }
    }
    .select-item {
      width: 100%;
    }
  }
</style>
<style>
  .el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 100%;
  }
  .el-input.is-disabled .el-input__inner {
    background-color:rgb(228,231,237,0.15);
    border-color: #E4E7ED;
    color: #606266;
  }
  .el-textarea.is-disabled .el-textarea__inner {
    background-color: rgb(228,231,237,0.15);
    border-color: #E4E7ED;
    color: #606266;
  }
</style>
